# 棋盘大小
BOARD_SIZE = 15

# 初始化棋盘
def init_board():
    return [[' ' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]

# 打印棋盘
def print_board(board):
    print("  ", end="")
    for i in range(BOARD_SIZE):
        print(f"{i:2d}", end="")
    print()
    for i in range(BOARD_SIZE):
        print(f"{i:2d}", end="")
        for j in range(BOARD_SIZE):
            print(f"{board[i][j]:2s}", end="")
        print()

# 判断落子是否合法
def is_valid_move(board, x, y):
    return 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and board[x][y] == ' '

# 判断是否有五子连成一线
def check_win(board, x, y, player):
    directions = [(1, 0), (0, 1), (1, 1), (1, -1)]
    for dx, dy in directions:
        count = 1
        # 正向检查
        i, j = x + dx, y + dy
        while 0 <= i < BOARD_SIZE and 0 <= j < BOARD_SIZE and board[i][j] == player:
            count += 1
            i += dx
            j += dy
        # 反向检查
        i, j = x - dx, y - dy
        while 0 <= i < BOARD_SIZE and 0 <= j < BOARD_SIZE and board[i][j] == player:
            count += 1
            i -= dx
            j -= dy
        if count >= 5:
            return True
    return False

# 判断是否平局
def is_draw(board):
    for row in board:
        if ' ' in row:
            return False
    return True

# 主游戏循环
def play_game():
    board = init_board()
    current_player = 'X'
    while True:
        print_board(board)
        print(f"当前玩家: {current_player}")
        try:
            x, y = map(int, input("请输入落子坐标 (x y): ").split())
            if is_valid_move(board, x, y):
                board[x][y] = current_player
                if check_win(board, x, y, current_player):
                    print_board(board)
                    print(f"玩家 {current_player} 获胜！")
                    break
                if is_draw(board):
                    print_board(board)
                    print("平局！")
                    break
                current_player = 'O' if current_player == 'X' else 'X'
            else:
                print("无效的落子，请重新输入。")
        except ValueError:
            print("输入无效，请输入两个整数。")

if __name__ == "__main__":
    play_game()